<script lang="ts" setup>
import { ref, nextTick } from 'vue'
import { ElMessage, ElTree } from 'element-plus'
import addDialog from '@/views/gdgl/gdzl/ysp/dialogs/add-dialog.vue'
import addPhotoDialog from '@/views/gdgl/gdzl/ysp/dialogs/add-photo-dialog.vue'
import { mlxxMainStore } from '@/store/mlxx'

const emit = defineEmits(['nodeClick'])
const mlxxStore = mlxxMainStore()

//获取ref
const addDialogRef = ref()
const addPhotoRef = ref()

const props = defineProps({
  syfsmc: {
    type: String,
    required: true,
  },
})

// 入参
const filterForm = ref({
  gjzValue: '',
})

// 生命周期
// onMounted(() => {
//   // 查询树
//   queryTree()
// })

const init = () => {
  // 查询树
  queryTree()
  treeNodeData.value = {}
}

const treeData = ref<any>([])
const treeRef = ref<InstanceType<typeof ElTree>>()
const firtsNode = ref<any>()
/*
 * 查询树
 */
const queryTree = () => {
  //处理数据
  const getTreeData = [
    {
      ...mlxxStore.daxqData,
      mc: `${mlxxStore.daxqData?.bmsah}${mlxxStore.daxqData?.ajmc}`,
    },
  ]
  treeData.value = getTreeData

  if (treeData.value.length) {
    firtsNode.value = null
    // 递归选择第一个
    getFirstNode(treeData.value)
    if (firtsNode.value) {
      handleCheckNode(firtsNode.value)
    }
  }
}

const getFirstNode = (arr: any[]) => {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].children && arr[i].children.length > 0) {
      getFirstNode(arr[i].children)
    } else {
      firtsNode.value = arr[i]
    }
    return
  }
}

const handleCheckNode = (node: any) => {
  treeNodeClick(node)
  nextTick(() => {
    treeRef.value!.setCurrentKey(node.dabh)
  })
}

// 文字过滤
const filterTree = () => {
  treeRef.value?.filter(filterForm.value.gjzValue)
}

/**
 * 树节点过滤
 */
const filterNode = (value: any, data: any) => {
  let flag = true
  if (value) {
    if (typeof value === 'string') {
      // 文字过滤
      flag = data[defaultProps.label].indexOf(value) !== -1
    }
  }
  return flag
}

/*
 * 树节点点击（抛出方法到主界面）
 */
const treeNodeData = ref()

const treeNodeClick = (data: any) => {
  treeNodeData.value = data
  emit('nodeClick', treeNodeData.value)
  mlClose()
}

/* 树选择 */
const menuShow = ref(false)

/* 关闭目录 */
const mlClose = () => {
  // 取消鼠标监听事件 菜单栏
  menuShow.value = false
  document.removeEventListener('click', mlClose) // 关掉监听，
}

const defaultProps = {
  children: 'children',
  label: 'mc',
}

const downloaClick = () => {
  window.location.href = `${window.IPCONFIG}/fpi/wjfw/download?wjbh=${mlxxStore.daxqData?.wjxh}`
}

// 打印
const printClick = () => {
  if (mlxxStore.daxqData?.dalx === 'spda')
    return ElMessage({ type: 'warning', message: '视频不允许打印' })
}

defineExpose({ init })
</script>
<template>
  <div class="container">
    <div class="center">
      <el-row :gutter="10">
        <el-col :span="mlxxStore.daxqData?.showDownloadPrinting ? 12 : 24">
          <el-input
            v-model="filterForm.gjzValue"
            autosize=""
            placeholder="请输入查询关键字"
            @change="filterTree"
          >
            <template #suffix>
              <el-icon @click="filterTree" class="el-input__icon"><Search /></el-icon>
            </template>
          </el-input>
        </el-col>
        <el-col :span="12" v-if="mlxxStore.daxqData?.showDownloadPrinting">
          <div style="display: flex">
            <button
              class="btn_operation"
              @click="downloaClick"
              :disabled="!props.syfsmc?.includes('下载')"
            >
              <div class="btn-bj" title="下载"></div>
              <span>下载</span>
            </button>
            <button
              class="btn_operation"
              :disabled="!props.syfsmc?.includes('打印')"
              @click="printClick"
            >
              <div class="btn-sbxz" title="打印"></div>
              <span>打印</span>
            </button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="bottom">
      <!--         @node-drop="handleDrop"
          :allow-drag="allowDrag"
          :allow-drop="allowDrop" -->
      <el-tree
        ref="treeRef"
        style="max-width: 600px"
        :data="treeData"
        node-key="dabh"
        highlight-current
        default-expand-all
        :expand-on-click-node="false"
        :props="defaultProps"
        show-checkbox
        @node-click="treeNodeClick"
        :filter-node-method="filterNode"
      >
        <template #default="{ data }">
          <div class="custom-tree-node">
            <!-- <el-icon v-if="data.fbh"> <FolderOpened /></el-icon> -->
            <!-- <img v-if="!data.fbh" src="/src/assets/images/file-icon.png" alt="" />
              <img v-if="data.cj === 1" src="/src/assets/images/text-icon.png" alt="" />
              <img v-if="data.mllxmc === '视频'" src="/src/assets/images/xssp.png" alt="" />
              <img v-if="data.mllxmc === '音频'" src="/src/assets/images/xsyp.png" alt="" />
              <img v-if="data.mllxmc === '照片'" src="/src/assets/images/tpwj.png" alt="" /> -->
            <!-- <el-icon v-if="data.zrz"> </el-icon> -->
            <span class="over-title" :title="data.mc">&nbsp;{{ data.mc }}&nbsp;</span>
            <span>{{ data.wjbh ? '(1)' : '(0)' }}</span>
          </div>
        </template>
      </el-tree>
      <!-- 树形控件右键组件 -->
      <!-- <div v-show="menuShow" class="tree_rightmenu" :style="{ ...rightMenu }">
          <el-button
            v-for="(item, index) in menuList"
            :key="index"
            type="primary"
            text
            :disabled="item.disabled"
            @click="menuButton(item)"
            >{{ item.text }}</el-button
          >
        </div> -->
    </div>
    <add-dialog ref="addDialogRef" @refresh="queryTree"></add-dialog>
    <add-photo-dialog ref="addPhotoRef" @refresh="queryTree"></add-photo-dialog>
  </div>
</template>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  .center {
    padding-bottom: 10px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.15);
  }
  .bottom {
    height: calc(100% - 43px);
    overflow-y: auto;
    .tree_rightmenu {
      position: fixed;
      width: 120px;
      border: 1px solid rgba(0, 0, 0, 0.15);
      box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
      z-index: 1000;
      background-color: #fff;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
  }
}
.add-ml {
  width: 100%;
}
.el-input__icon {
  cursor: pointer;
}
.custom-tree-node {
  width: calc(100% - 80px);
  display: flex;
  align-items: center;
  .over-title {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}
:deep(.el-tabs .el-tabs__content) {
  height: calc(100% - 40px);
  /* overflow: auto; */
  padding: 0;
}

.el-button + .el-button {
  margin-left: 0;
}
</style>
